import {APP_LOGIN, SET_OPEN_KEYS, SET_USER_INFO, TOGGLE_MENU} from 'redux/constant/app'
import lodash from 'lodash'
import {appState, IAppState} from 'redux/state/app'
import {MENU_OPEN_KEYS, USER_INFO} from "../../constant/app";

const app = (state=appState, action:any) => {
    let newState:IAppState=null
    switch (action.type) {
      case APP_LOGIN:

        newState=lodash.cloneDeep(state)
        newState.loginSuccessUri=action.payload
        return newState
      case TOGGLE_MENU:
         newState=lodash.cloneDeep(state)
         newState.menuCollapsed=!newState.menuCollapsed;
         return newState
      case SET_USER_INFO:
          console.log(action.payload)
         newState=lodash.cloneDeep(state)
         newState.userInfo=action.payload;
         localStorage.setItem(USER_INFO,JSON.stringify(action.payload))
         return newState
    case SET_OPEN_KEYS:
        newState=lodash.cloneDeep(state)
        newState.openKeys=action.payload;
        localStorage.setItem(MENU_OPEN_KEYS,JSON.stringify(action.payload))
        return newState
      default:
        return state
    }
}

export default app
